package com.aijk.web.admin.controller.info;

import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.EhmInfoClassifyApi;
import com.aijk.ehm.model.EhmInfoClassify;
import com.aijk.ehm.model.qo.EhmInfoClassifyQo;
import com.aijk.ehm.sys.model.SysAdmin;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 资讯分类
 */
@Controller
@RequestMapping(value = "/admin/info/infoClassify")
public class InfoClassifyController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(InfoClassifyController.class);

    @Autowired
    private EhmInfoClassifyApi mhInfoClassifyApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "info/infoClassify/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        draw += 1;
        EhmInfoClassifyQo qo = DataTableUtil.fillQo(skey, EhmInfoClassifyQo.class);
        PageInfo<EhmInfoClassify> pageInfo = mhInfoClassifyApi.page(qo);
        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", pageInfo.getList());
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }


    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        EhmInfoClassify bean = new EhmInfoClassify();
        if (id != null){
            bean = mhInfoClassifyApi.findOne(id);
        }
        model.addAttribute("mhInfoClassify", bean);
        return "info/infoClassify/edit";
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(EhmInfoClassify bean) {
        try {
            	Long id = bean.getId();

            SysAdmin admin = getCurrentUser();
            	if (id == null) {// 添加
                    bean.setOperator(admin.getName());
                    bean.setStatus((short) 1);
                    bean.setInfoNum(0);

            		mhInfoClassifyApi.save(bean);
            	} else {
                    bean.setOperator(admin.getName());
                    mhInfoClassifyApi.update(bean);
                	}
            }catch (ErrorCodeException e){
                	e.printStackTrace();
                	logger.error(e.getMessage());
                	return RetMsg.failed("请输入正确的排序");
            }
            return RetMsg.success();
        }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg deleteById(@RequestParam(value = "id") Long id) {
        try {
            	mhInfoClassifyApi.deleteById(id);
        }catch (ErrorCodeException e){
            	e.printStackTrace();
            	logger.error(e.getMessage());
            	return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/grounding", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg grounding(@RequestParam(value = "id") Long id){
        EhmInfoClassify basBanner = mhInfoClassifyApi.findOne(id);
        if(basBanner != null){
            short status = basBanner.getStatus();
            if(status == 1){
                status = 0;
            }else{
                status = 1;
            }
            EhmInfoClassify updateBanner = new EhmInfoClassify();
            updateBanner.setId(id);
            updateBanner.setStatus(status);
            mhInfoClassifyApi.update(updateBanner);
        }
        return RetMsg.success();
    }


}